package org.ilc.socketserver;

import org.ilc.push.IHeader;
import org.ilc.util.ValidateUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息头
 * @author malongbo
 */
public class Header implements IHeader {
    public static final String HEAD_SERVER_HOST_KEY = "host";
    private Map<String, String> data = null;

    public Header() {  }

    /**
     *
     * @param data
     * @throws Exception
     */
    public Header(byte[] data) throws Exception {
        // this(CipherUtil.decrypt(new String(data, HEADER_ENCODING_CHARSET)));
        this(new String(data, HEADER_ENCODING_CHARSET));
    }

    /**
     *
     * @param headerString
     */
    public Header(String headerString) {
        if (headerString != null && !"".equals (headerString.trim ())) {
            data = new HashMap<String, String> ();

            String[] headerArray = headerString.trim().split("\n");

            for (String headerPair : headerArray) {

                if("".equals (headerPair.trim ())){
                    continue;
                }

                int firstEqIndex = headerPair.indexOf(":");

                if (firstEqIndex == -1)
                    throw new RuntimeException("非法的头信息");

                String name = headerPair.substring(0, firstEqIndex);
                String value = headerPair.substring(firstEqIndex + 1);

                /*
                用户信息解密
                 */
                if(HEAD_AUTH_USER_KEY.equalsIgnoreCase(name) || HEAD_AUTH_PWD_KEY.equalsIgnoreCase(name)){
                    try {
//						value= CipherUtil.decrypt (value);
                    } catch (Exception e) {
                        throw new RuntimeException ("用户信息解密失败");
                    }
                }
                data.put(name, value);
            }
        }
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public String getHeader(String key) {
        return null!=data?data.get(key):null;
    }

    /**
     *
     * @return
     */
    public int getContentLength() {
        String value = getHeader(HEAD_CONTENT_LENGTH_KEY);
        int length = 0;
        try{
            length = Integer.parseInt(value);
        }catch(Exception e){
            length = 0;
        }
        return length;
    }

    /**
     *
     * @param name
     * @param value
     */
    public void addHeader(String name, String value) {
        if(null==data){
            data = new HashMap<String, String>();
        }
        if (ValidateUtil.validateStr (name))
            data.put(name.toLowerCase(), value);
    }

    public String toString() {
        if (null == data)
            return "";

        StringBuffer hreader = new StringBuffer();

        for (String name : data.keySet()) {
            hreader.append(name).append(":").append(data.get(name))
                    .append("\n");
        }

        return hreader.toString();
    }

    public Map<String, String> getHeaders() {
        return data;
    }

    @Override
    public String getAuthUser() {
        return getHeader(IHeader.HEAD_AUTH_USER_KEY) == null ?
                getHeader(IHeader.HEAD_AUTH_USER_KEY) : getHeader(IHeader.HEAD_AUTH_USER_KEY).toLowerCase();
    }

    @Override
    public String getAuthPwd() {
        return getHeader(IHeader.HEAD_AUTH_PWD_KEY);
    }

    @Override
    public void destroy() {
        if(null!=data){
            data.clear();
            this.data=null;
        }
    }
}
